package com.itheima.io.utils.demo02;

import java.io.*;

public class IOUtils {

    public static void copyFileWithCharStream(Reader reader, Writer writer){

        try{
            char[] chars = new char[1024];
            int len = -1;
            while ((len = reader.read(chars)) != -1) {
                writer.write(chars);
            }

        } catch (IOException e){
            e.printStackTrace();
        }  finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void copyFileWithByteStream(InputStream input, OutputStream output){


        try{
            int by = -1;
            while ((by = input.read()) != -1){
                output.write(by);
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (output != null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }


}
